//
//  MCModifyNickNameViewController.swift
//  WisdomSpace
//
//  Created by goulela on 2017/9/8.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

class MCModifyNickNameViewController: MCViewController {

    
    var name : String?
    var type = 0
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        nameTextField.becomeFirstResponder()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        nameTextField.resignFirstResponder()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        manageClosure()
        
        sendNetworking()
        
        nameTextField.text = MCUserinfo_getNickName()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc func rightItemClicked() {
    
        if nameTextField.text?.count == 0 {
            MCHUD_text(message: "请填写昵称", view: self.view, delay: 1.2)
            return
        }
        
        let para = [
            "userId"   : MCUserinfo_getUserId(),
            "nickName" : nameTextField.text ?? ""
        ] as [String : Any]
        
        MCNetwork_post(url: JAVA_updateNickName, para: para as [String : AnyObject], vc: self, hudSuperView: self.view, isHiddenTabbar: false, success: { (response) in
            
            MCHUD_custom(message: "修改成功", view: self.view, delay: 1.5, image: "Reuse_complete")
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1.5, execute: { 
                self.navigationController?.popViewController(animated: true)
            })
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Network
    func sendNetworking() {
        
    }
    
    // MARK: - Closure
    func manageClosure() {
        
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "修改昵称"
        self.view.backgroundColor = UIColor.MCGray_light
        
        
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_text("确定", target: self, selector: #selector(rightItemClicked))        
    }
    
    func initUI() {
        view.addSubview(nameTextField)
        nameTextField.snp.remakeConstraints { (make) -> Void in
            make.left.equalTo(view).offset(-1)
            make.right.equalTo(view).offset(-1)
            make.top.equalTo(view).offset(-1)
            make.height.equalTo(40)
        }
    }
    
    
    // MARK: - Setter & Getter

    lazy var nameTextField: MCTextFiled = {
        let textField = MCTextFiled()
        textField.backgroundColor = UIColor.white
        textField.font = MCFont(14)
        textField.placeholder = "请输入昵称"
        textField.layer.borderWidth = 1
        textField.layer.borderColor = UIColor.lightText.cgColor
        
        textField.clearButtonMode = UITextFieldViewMode.whileEditing
        textField.delegate  = self
        textField.returnKeyType = UIReturnKeyType.done
        
        let view = UIView()
        view.frame = CGRect.init(x: 0, y: 0, width: 20, height: 20)
        textField.leftView = view
        textField.leftViewMode = UITextFieldViewMode.always
        return textField
    }()

}

extension MCModifyNickNameViewController : UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        
        self.view.endEditing(true)
        return true
    }
}
